package com.pms.framework;

import java.io.IOException;
import java.util.Hashtable;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class FrontController extends HttpServlet {



	private Hashtable htActionMapping = null;

	/**
	 * Constructor of the object.
	 */
	public FrontController() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * This Method is responsible for each every request comes from the user. It
	 * takes the page id & event id from the request according to that it picks
	 * up the appropriate validator & action classes & call them respectively.
	 * Than action class returns the token for the next screen which has to be
	 * shown to the user. It also checks for isForwardOnly & decide wether or
	 * not to do prcessing of the request. If validator class throws some error
	 * than it will by default go to the failure page.
	 * 
	 * 
	 */

	public void service(HttpServletRequest request, HttpServletResponse response) {
		try {
			
			
			RequestDispatcher rd = null;
			//rem for revert back to the normal condition by yagnesh on 20 jan 06
			//RequestValueObject requestValueObject = new RequestValueObject();
			String pageId=null;
			String eventId=null;
			if(request.getAttribute("pageId")!=null){
				pageId = (String)request.getAttribute("pageId");
				eventId = (String)request.getAttribute("eventId");
			}else{
			 pageId = request.getParameter("pageId");
			eventId = request.getParameter("eventId");
			}
			//System.out.println("Page:-" + pageId + " and Event:-" + eventId);
			ActionMap actionMap = (ActionMap) htActionMapping.get(pageId + "_" + eventId);
		
			if (actionMap == null) {
				System.out.println("There was no Action Map for Page:-" + pageId + " and Event:-" + eventId);
				//throw new Exception("No action mapped for the Event of the Page");
				Hashtable error = new Hashtable();
				error.put("NoPageMapped", "There was no Action Map for Page:-" + pageId + " and Event:-" + eventId);
				request.setAttribute("error", error);
				rd = request.getRequestDispatcher("/jsp/Error.jsp");
				rd.forward(request, response);
				return;
			}
			boolean forwardOnly = actionMap.isForwardOnly();

			
			if (forwardOnly) {
				request.setAttribute("nextPageToken", "success");
				rd = request.getRequestDispatcher("/servlet/ScreenFlowManagerServlet");
				rd.include(request, response);
				rd = request.getRequestDispatcher("/servlet/TemplateServlet");
				rd.forward(request, response);
			} else {
				Validator validator = actionMap.getValidatorClass();
				//System.out.println( "Validator Class - "+ validator.toString());
				Hashtable error = validator.validate(request, response);
				request.setAttribute("error",error);
				Action action = actionMap.getActionClass();
				System.out.println( "Executing action class - "+ action.toString());
				action.execute(request,response);
				rd = request.getRequestDispatcher("/servlet/ScreenFlowManagerServlet");
				rd.include(request, response);
				rd = request.getRequestDispatcher("/servlet/TemplateServlet");
				rd.forward(request,response);
			}


		} catch (IOException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Initialization of the servlet. <br> & if it's the first request than it
	 * goes in the ActionMapping.xml file takes the all mapping for all action &
	 * stroes it in the Hashtable keeps it in the application context & from the
	 * next time onwards goes in application &
	 * 
	 * @throws ServletException
	 *             if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here

		ServletContext application = getServletContext();
		String resourcePath = application.getRealPath("/");
		resourcePath += "/WEB-INF/ActionMapping.xml";
		ActionMappingXmlLoader actionMappingXmlLoader = ActionMappingXmlLoader.getInstance();
		htActionMapping = actionMappingXmlLoader.loadSettings(resourcePath);
		application.setAttribute("actionMapping",htActionMapping);
		
		/*Agent agent = AgentDao.getInstance().findByPrimaryKey(1000);
		application.setAttribute("onlineUserObject",agent);*/
	}

}
